<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <title>canvas</title>
    <style>
        body{
            padding:0;
            margin:0;
        }
        canvas{
            background:#eee;
            padding:0;
        }
        div{
            color: #000;
            background: #ddd;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
     <p id="log"></p>
    <script src='../js/ball.js'></script>
    <script src="../js/arrow.js"></script>
    <script src='../js/utils.js'></script>
    <script>
        window.onload = function(){
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext('2d');

            canvas.width = document.body.clientWidth;
            canvas.height = window.screen.availHeight*0.8;
            
            var vx = Math.random()*10 - 5;
             var vy = Math.random()*10 - 5;

             var ball = new Ball(20, "#ff0000");
                 ball.x = canvas.width/2;
                 ball.y = canvas.height/2;
                
            //动画循环
             (function drawFrame(){
                 window.requestAnimationFrame(drawFrame, canvas);
                 context.clearRect(0, 0, canvas.width, canvas.height);

                 ball.x += vx;
                 ball.y += vy;
                 
                //核心部分
                 if(ball.x + ball.radius > canvas.width){
                     ball.x = canvas.width - ball.radius;
                     vx *= -1;
                 }else if(ball.x - ball.radius < 0){
                     ball.x = ball.radius;
                     vx *= -1;
                 }
                 if(ball.y + ball.radius > canvas.height){
                     ball.y = canvas.height - ball.radius;
                     vy *= -1;
                 }else if(ball.y - ball.radius < 0){
                     ball.y = ball.radius;
                     vy *= -1;
                 }

                 ball.draw(context);
             }())

        }
    </script>
</body>
</html>
